﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;


namespace JigsawService.Utility
{
    public class FileStorage
    {
        public FileStorage()
        {

        }

        /// <summary>
        /// returns the number of bytes in a file as long integer value
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public long GetFileSize(string fullPath)
        {
            long fileSize = 0;

            if (File.Exists(fullPath))
            {
                FileInfo fi = new FileInfo(fullPath);
                Stream s = new FileStream(fi.FullName.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);
                fileSize = s.Seek(0, SeekOrigin.End);
                fi = null;
                s.Close();
                s = null;
            }

            return fileSize;
        }

        /// <summary>
        /// reads a block of 1,000,000 bytes from the specified file,
        /// or the entire file if smaller than 1,000,000 bytes.
        /// </summary>
        /// <param name="localPath"></param>
        /// <param name="offSet"></param>
        /// <returns></returns>
        public byte[] ReadFileBlock(string localPath, long offSet)
        {
            byte[] TmpBuffer = new byte[0];
            int BytesRead = 0;
            int blockSize = 1000000;

            if (localPath != null && localPath != "" && offSet >= 0)
            {
                using (FileStream fs = new FileStream(localPath, FileMode.Open, FileAccess.Read))
                {
                    fs.Seek(offSet, SeekOrigin.Begin);
                    TmpBuffer = new byte[blockSize];
                    BytesRead = fs.Read(TmpBuffer, 0, blockSize);

                    fs.Close();
                }

                // the last chunk will not fill the buffer, so it must be trimmed before returning
                if (BytesRead != blockSize)
                {
                    byte[] TrimmedBuffer = new byte[BytesRead];
                    Array.Copy(TmpBuffer, TrimmedBuffer, BytesRead);
                    TmpBuffer = TrimmedBuffer;
                }
            }
            
            return TmpBuffer;
        }

        /// <summary>
        /// Appends a block of data to a file,
        /// or writes the entire file if smaller than 1,000,000 bytes.
        /// </summary>
        /// <param name="localPath"></param>
        /// <param name="fileBlock"></param>
        public bool WriteFileBlock(string localPath, byte[] fileBlock)
        {
            bool writeBlock = false;

            // check to see if file already exists
            if (!File.Exists(localPath))
            {
                // create file
                File.Create(localPath).Close();
            }

            // append block to end of the file
            using (FileStream fs = new FileStream(localPath, FileMode.Append))
            {
                fs.Write(fileBlock, 0, fileBlock.Length);
                writeBlock = true;
            }

            return writeBlock;
        }

    }
}